<?php

/**
 * 模型对应数据库表 "web_adsmanage_sort".
 *
 * 以下是可用表web_adsmanage_sort的栏位:
 * @property string $id
 * @property string $ads_name
 * @property integer $ads_order
 * @property string $ads_height
 * @property string $ads_width
 * @property integer $ads_webset
 * @property string $start_time
 * @property string $end_time
 * @property integer $ads_start
 */
class WebAdsmanageSort extends CActiveRecord {

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return WebAdsmanageSort the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'web_adsmanage_sort';
    }

    /**
     * 返回验证规则 
     * @author nirsh 
     * @version 5.0 
     * @example 
     */
    public function rules() {
        return array(
            array('ads_name, ads_height, ads_width, ads_start, ads_type', 'required'),
            array('ads_order, ads_start, ads_type, ads_height, ads_width', 'numerical', 'integerOnly' => true),
            array('ads_name', 'length', 'max' => 100),
            array('start_time, end_time', 'safe'),
            // The following rule is used by search(). 
            // Please remove those attributes that should not be searched. 
            array('id, ads_name, ads_order, ads_height, ads_width, ads_webset, start_time, end_time, ads_start, ads_type', 'safe', 'on' => 'search'),
        );
    }

    public $ads_start_array = array(
        0 => '启用',
        1 => '禁用',
    );

    public $ads_type_array = array(
        1 => '单图片',
        2 => '滚动图片',
    );

    /**
     * 激活状态标志
     * @param 状态id
     * @author nirsh
     */
    public function getCustomerState($aData) {
        $sUrl = Yii::app()->createUrl('WebAdsmanageSort/setImportant', array(
            'id' => $aData->id,
            'state' => $aData->ads_start,
                ));
        $adsManage_state = $aData->ads_start;
        if ($adsManage_state) {
            $rString = '<a href="' . $sUrl . '" class="important" title="点击禁用"></a>';
        } else {
            $rString = '<a href="' . $sUrl . '" class="set-important" title="点击启用"></a>';
        }
        return $rString;
    }

    /**
     * 选择判断激活
     * @param type $col   
     */
    public function resetState() {
        $sql = " UPDATE web_adsmanage SET state= 0 WHERE state=1";
        Yii::app()->db->createCommand($sql)->execute();
    }

    /**
     * 获取广告位分类
     * @return type 
     */
    public function getAll() {
        $data = array();
        foreach ($this->findAll() as $v) {
            $data[$v->id] = $v->ads_name;
        }
        return $data;
    }

    /*
      +-----------------------------------------------------
     * 说明 获取操作菜单
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */

    public function getColumnOptions($data) {

        $vUrl = Yii::app()->controller->rewriteCreateUrl('webAdsmanageSort/view', array('id' => $data->id));
        $uUrl = Yii::app()->controller->rewriteCreateUrl('webAdsmanageSort/update', array('id' => $data->id));
        $sUrl = Yii::app()->createAbsoluteUrl('webAdsmanageSort/AdsShow', array('id' => $data->id));
        $gUrl = Yii::app()->controller->rewriteCreateUrl('webAdsmanage/admin', array('WebAdsmanage[sort]' => $data->id));

        $view = '<a href="#inline_content"  sUrl="' . $sUrl . '"  onclick="getid(this)" class="gradient-btn boxgold">获取代码</a>';
        $view.= '<a title="广告内容管理" href="' . $gUrl . '" class="gradient-btn">内容管理</a>';
        $editor = '<a title="修改" href="' . $uUrl . '" class="gradient-btn">修改</a>';
        $editor.= '<a title="查看" href="' . $vUrl . '" class="gradient-btn">查看</a>';
        return $view . $editor;
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array();
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'ads_name' => '广告位名称',
            'ads_order' => '排序',
            'ads_height' => '高度(px)',
            'ads_width' => '宽度(px)',
            'ads_type' => '类型',
            'start_time' => '开始时间',
            'end_time' => '结束时间',
            'ads_start' => '状态',
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        $criteria->compare('id', $this->id);
        $criteria->compare('ads_name', $this->ads_name, true);

        $criteria->compare('start_time', $this->start_time);
        $criteria->compare('end_time', $this->end_time);
        $criteria->compare('ads_start', $this->ads_start);

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

}
